package wht.jly.forum.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import wht.jly.forum.domain.User;

import java.util.List;

/**
 * @author lanyangji
 * @date 2019/2/8 21:06
 */
public interface UserRepository extends JpaRepository<User, Integer>, QuerydslPredicateExecutor<User> {

    /**
     * 按照姓名和密码查询
     *
     * @param username
     * @param password
     * @return
     */
    User findByUsernameAndPassword(String username, String password);

    /**
     * 按照姓名查询
     *
     * @param username
     * @return
     */
    User findByUsername(String username);

    /**
     * 根据用户ID,查询用户所拥有角色码
     *
     * @param userId
     * @return
     */
    @Query("select r.roleCode from User u " +
            "inner join UserRole ur on u.id = ur.userId " +
            "inner join Role r on ur.roleId = r.id " +
            "where u.id = :userId")
    List<String> findRoleCodesByUserId(Integer userId);

}
